package com.novoda.runbuddy.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.novoda.runbuddy.R;
import com.novoda.runbuddy.controller.CurrentRunMapControllerImpl;
import com.novoda.runbuddy.controller.ICurrentRunMapController;
import com.novoda.runbuddy.controller.IOptionsMenu;
import com.novoda.runbuddy.controller.OptionsMenuImpl;
import com.novoda.runbuddy.provider.ActivityID;
import com.novoda.runbuddy.service.RemoteStopClockServiceInterface;

public class CurrentRunMap extends MapActivity {

	private ICurrentRunMapController	mapOfJogController;
	public LocationManager				locationManager		= null;
	protected MyIntentReceiver			myIntentReceiver	= null;
	final Handler						mHandler			= new Handler();
	private static final String			TAG					= "[RBuddy]: " + "CurrentRunMap";
	private IOptionsMenu				optionsMenu;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		this.setContentView(R.layout.current_run_map_view);
		optionsMenu = new OptionsMenuImpl(this);

		this.myIntentReceiver = new MyIntentReceiver();

		mapOfJogController = new CurrentRunMapControllerImpl(this);

		mapOfJogController.createMapOfJogView((MapView) findViewById(R.id.map));

		mapOfJogController.startTrackingRoute();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return mapOfJogController.onKeyDown(keyCode, event);
	}

	public void stopStopwatchService(ServiceConnection connection) {
		mapOfJogController.setServiceRunning(false);
	}

	class MyIntentReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {

			if (intent.getAction().equals(ActivityID.MY_LOCATION_CHANGED_ACTION)) {
			}

			if (intent.getAction().equals(ActivityID.STOPWATCH_STARTED)) {
				Log.v(TAG, "!!!!! This is the intent Action :" + intent.getAction().toString());
				mapOfJogController.startTrackingRoute();
			}

			if (intent.getAction().equals(ActivityID.STOPWATCH_STOPPED)) {
				Log.v(TAG, "!!!!! This is the intent Action :" + intent.getAction().toString());
				mapOfJogController.stopTrackingRoute();
			}

		}

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onResume() {
		Handler msgHandler = mapOfJogController.getUpdateHandler();
		RemoteStopClockServiceInterface.getInstance().setUpdateHandler(msgHandler);

		mapOfJogController.onResume();

		super.onResume();
	}

	@Override
	protected void onPause() {
		RemoteStopClockServiceInterface.getInstance().clearUpdateHandler(mapOfJogController.getUpdateHandler());

		super.onPause();
	}

	@Override
	protected void onStart() {
		super.onStart();
		RemoteStopClockServiceInterface.getInstance().registerListener(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		RemoteStopClockServiceInterface.getInstance().unregisterListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		optionsMenu.addAllActivitiesToMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		optionsMenu.selectItem(item);
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

}